package bb.lanxing.device.sgdevcie.setting.fragment.adapter;

import java.util.ArrayList;
import java.util.List;

import com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings.storagemanager.LaxingRouteBookManagerFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import bb.lanxing.R;
import bb.lanxing.databinding.LayoutLxRoutebookManagerItemBinding;
import bb.lanxing.databinding.RouteEmptyViewBinding;
import bb.lanxing.lib.devices.sgsettingentity.routebook.Route;

public class RouteBookManageAdapter extends BaseQuickAdapter<Route, BaseViewHolder> implements LoadMoreModule {
    private boolean isEditMode = true;//true;//test
    private static final String TAG = "RouteBookManageAdapter";
    LaxingRouteBookManagerFragment laxingRouteBookManagerFragment = null;
    public boolean isAllChecked() {
        return isAllChecked;
    }

    public void setAllChecked(boolean allChecked) {
        isAllChecked = allChecked;
    }

    private boolean isAllChecked = false;
    public RouteBookManageAdapter() {
        super(R.layout.layout_lx_routebook_manager_item);
    }

    public RouteBookManageAdapter(int layoutResId) {
        super(layoutResId);
    }

    public RouteBookManageAdapter(int layoutResId, @Nullable List<Route> data) {
        super(layoutResId, data);
    }

    public RouteBookManageAdapter(LaxingRouteBookManagerFragment fragment) {
        super(R.layout.layout_lx_routebook_manager_item);
        laxingRouteBookManagerFragment = fragment;
    }
/*
    @Override
    protected void convert(BaseDataBindingHolder<LayoutLxRoutebookManagerItemBinding> holder, final Route item) {
        LayoutLxRoutebookManagerItemBinding dataBinding = holder.getDataBinding();
        Log.d(TAG," the route item entry: " );
        if (dataBinding != null) {
//            dataBinding.setIsEditMode(this.isEditMode);
//            dataBinding.setRoute(item);
            //dataBinding.set
            dataBinding.llRoot.setOnClickListener(view -> {
                if (isEditMode) {
                    Log.d(TAG," the route item status111: " + item.getChecked() + "name " + item.getName());
                    item.setChecked(!item.getChecked());
                    //add by wangsm
                    if(laxingRouteBookManagerFragment != null){
                        laxingRouteBookManagerFragment.updateFragmentViews();
                    }
                    //end by wangsm
                    notifyItemChanged(getItemPosition(item));
                  //  Log.d(TAG," the route item notify: " + item.getChecked());
                }
            });
           // Log.d(TAG," the route item status: " + item.getChecked());
            dataBinding.routeCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  //  Log.d(TAG," the route item onCheckedChanged: " + isChecked);
                    if(laxingRouteBookManagerFragment != null){
                        laxingRouteBookManagerFragment.updateFragmentViews();
                    }
                }
            });
            dataBinding.routeCheckbox.setChecked(item.getChecked());
            dataBinding.routeFileId.setText(item.getRid());
            dataBinding.routeFileSize.setText(String.valueOf(item.getSize()));
        }

    }*/



    public final boolean isEditMode() {
        return this.isEditMode;
    }

    public final void setEditMode(boolean z) {
        this.isEditMode = z;
        notifyDataSetChanged();
    }

    public final void checkAll() {
        for (Route route : getData()) {
            route.setChecked(true);
        }
        setAllChecked(true);
        notifyDataSetChanged();
    }

    public final void clearChecked() {
        for (Route route : getData()) {
            route.setChecked(false);
        }
        setAllChecked(false);
        notifyDataSetChanged();
    }

    public final List<Route> getCheckedItem() {
        ArrayList arrayList = new ArrayList();
        for (Route obj : getData()) {
            if (obj.getChecked()) {
                arrayList.add(obj);
            }
        }
        return arrayList;
    }

    public final boolean unCheckedItemExist() {
        for (Route obj : getData()) {
            if (!obj.getChecked()) {
                return true;
            }
        }
        return false;
    }

    public final List<Route> getCheckedRouteList() {
        List<Route> routes = new ArrayList<>();
        for (Route obj : getData()) {
            if (obj.getChecked()) {
                routes.add(obj);
            }
        }

        return routes.size() >0 ? routes: null;
    }

    public final List<String> getCheckedRouteRidList() {
        List<String> ridList = null;
        List<Route> routes = getCheckedRouteList();
        if(routes != null){
            ridList = new ArrayList<>();
            for(Route route : routes){
                String filename = route.getRid()+ "." + route.getExt();
                Log.d(TAG," the checked routebook file name: " + filename);
                ridList.add(filename);
            }
        }

        return !ridList.isEmpty() ? ridList: null;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {//RecyclerView.ViewHolder
        LayoutInflater from = LayoutInflater.from(parent.getContext());
        if (viewType == EMPTY_VIEW) {
            Log.d(TAG," the view is empty view");
            return new RouteManagerEmptyViewHolder(DataBindingUtil.inflate(from, R.layout.route_empty_view, parent, false));
        }else{
            Log.d(TAG," the view is not empty view");
            return new RouteLayoutItemHolder(DataBindingUtil.inflate(from, R.layout.layout_lx_routebook_manager_item, parent, false));

        }
    }
    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, Route item) {
        if(baseViewHolder instanceof RouteLayoutItemHolder ){
            Log.d(TAG," show list view");
            LayoutLxRoutebookManagerItemBinding dataBinding = ((RouteLayoutItemHolder)baseViewHolder).getBinding();
            if(dataBinding != null){
                dataBinding.llRoot.setOnClickListener(view -> {
                    if (isEditMode) {
                        Log.d(TAG," the route item status111: " + item.getChecked() + "name " + item.getName());
                        item.setChecked(!item.getChecked());
                        //add by wangsm
                        if(laxingRouteBookManagerFragment != null){
                            laxingRouteBookManagerFragment.updateFragmentViews();
                        }
                        //end by wangsm
                        notifyItemChanged(getItemPosition(item));
                        //  Log.d(TAG," the route item notify: " + item.getChecked());
                    }
                });
                // Log.d(TAG," the route item status: " + item.getChecked());
                dataBinding.routeCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        //  Log.d(TAG," the route item onCheckedChanged: " + isChecked);
                        if(laxingRouteBookManagerFragment != null){
                            laxingRouteBookManagerFragment.updateFragmentViews();
                        }
                    }
                });
                Log.d(TAG," show item");
                dataBinding.routeCheckbox.setChecked(item.getChecked());
                dataBinding.routeFileId.setText(item.getRid());
                dataBinding.routeFileSize.setText(String.valueOf(item.getLength()));
            }
        }else if(baseViewHolder instanceof RouteManagerEmptyViewHolder ){
            Log.d(TAG," show route empty view");
        }else {
            Log.d(TAG," show nothing ");
        }

    }




    private static class RouteManagerEmptyViewHolder extends BaseViewHolder  {
        private final RouteEmptyViewBinding routeEmptyViewBinding;

        private RouteManagerEmptyViewHolder(RouteEmptyViewBinding binding) {
            super(binding.getRoot());
            routeEmptyViewBinding = binding;
        }

        public RouteEmptyViewBinding getEmptyViewBinding() {
            return routeEmptyViewBinding;
        }
    }
    private static class RouteLayoutItemHolder extends BaseViewHolder  {
        private final LayoutLxRoutebookManagerItemBinding layoutLxRoutebookManagerItemBinding;

        private RouteLayoutItemHolder(LayoutLxRoutebookManagerItemBinding binding) {
            super(binding.getRoot());
            layoutLxRoutebookManagerItemBinding = binding;
        }

        public LayoutLxRoutebookManagerItemBinding getBinding() {//getRoutebookItemBinding()
            return layoutLxRoutebookManagerItemBinding;
        }
    }
}
